package com.tinysand.advert.entity;

import com.tinysand.advert.constant.CommonStatus;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.Date;

/**
 * 广告投放系统模块推广计划实体类定义（正常企业开发中一般不使用外键来维护两个表
 * 之间的关联关系，会导致两表之间耦合度高；所以一般会使用逻辑外键而不是物理外键）
 *
 * Created By TINY
 * Create Time 2020/2/15
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "ad_plan")
@EntityListeners(AuditingEntityListener.class) // 使用JPA审计维护日期字段
public class AdvertPlan {

    /* 推广计划表自增主键 */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    /* 关联用户id */
    @Column(name = "user_id", nullable = false)
    private Long userId;

    /* 推广计划名称 */
    @Column(name = "plan_name", nullable = false)
    private String name;

    /* 推广计划状态 */
    @Column(name = "plan_status", nullable = false)
    private Integer planStatus;

    /* 推广计划的开始时间 */
    @Column(name = "start_date", nullable = false)
    private Date startDate;

    /* 推广计划的结束时间 */
    @Column(name = "end_date", nullable = false)
    private Date endDate;

    @Column(name = "create_time", nullable = false)
    @CreatedDate // 使用JPA审计功能维护记录创建时间
    private Date createTime;

    @Column(name = "update_time", nullable = false)
    @LastModifiedDate // 使用JPA审计功能自动维护更新时间
    private Date updateTime;

    public AdvertPlan(Long userId, String planName,
                      Date startDate, Date endDate) {
        this.userId = userId;
        this.name = planName;
        this.planStatus = CommonStatus.VALID.getStatus();
        this.startDate = startDate;
        this.endDate = endDate;
    }
}
